<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ConsultService
 *
 * @author lzx
 */
require_once BaseUtil::getModulePath("dbgrid").'/DBGrid.php';
require_once BaseUtil::getModulePath("dbgrid").'/DBGridConstant.php';
require_once BaseUtil::getModulePath('base').'/services/BaseService.php';
require_once BaseUtil::getModulePath('consult').'/models/QueryReplyDao.php';
require_once BaseUtil::getModulePath('consult').'/models/QueryDao.php';
require_once BaseUtil::getModulePath('consult').'/constants/ConsultConstant.php';
require_once BaseUtil::getModulePath('component').'/Number.php';

class ConsultService extends BaseService{
    //put your code here
    private $queryreplyDao;
    private $queryDao;
    
    public function __construct(){
        parent::__construct();
        $this->queryreplyDao = new QueryReplyDao();
        $this->queryreplyDao->setDb($this->db);
        $this->queryDao = new QueryDao();
        $this->queryDao->setDb($this->db);
    }
    
    //It's the answer of the customers's questions.
    public function editConsult($form,$queryId){
        $consult=array();
//        $query = array();
        $consult["query_id"]=$queryId;
//        $consult["reply_id"]=$form->getValue("id");
//        $consult["reply_id"]=$form->getValue("replyId");//this value is 0 ??
        $consult["reply_id"]=$form->getValue("hReplayId");
        $consult["content"]=$form->getValue("content");
        $queryContent["status"] = 2;
        $queryWhere = $this->db_w->quoteInto('id = ?', $queryId);
//        if(empty($consult["reply_id"])){
            $this->queryreplyDao->insertData($consult);
            $this->queryDao->updateData($queryContent, $queryWhere);
//        }else{
//            $this->queryreplyDao->updateData($consult, $consult["query_id"]);
//        }
    }
    
    //The question is base on the ourself consult
    public function addConsult($form){
        //insert queries
        $consult=array();
        $offset=rand(20,40);
        $consult["query_type"]=$form->getValue("type");
        $consult["customer_id"]=$form->getValue("customerId");//query by myself,so this customer id is false
        $consult["content"]=$form->getValue("question");
//        $consult["product_item_id"]=$form->getValue("item");
//        $consult["product_id"]=$form->getValue("hProductId");
        $consult["product_id"]=$form->getValue("item");
        $consult["status"]=2;
        $consult["created"]=date("Y-m-d H:i:s",strtotime("-$offset minutes"));
        $consult["modified"]=date("Y-m-d H:i:s",strtotime("-$offset minutes"));
        $id = $this->queryDao->insert($consult);
        //insert reply
        //$user=?;
        $consultreply=array();
        $consultreply["query_id"]=$id;
        $consultreply["reply_id"]=$form->getValue("hReplayId");
        $consultreply["content"]=$form->getValue("reply");
        $id = $this->queryreplyDao->insertData($consultreply);
    }
    
    //delete table queries
    public function deleteQueries($id){
        $this->queryDao->deleteQuery($id);
        $this->queryreplyDao->deleteQueryReply($id);
    }
    
    //Select table queries
    public function getQueries($view,$form){
        $selectOptions = array("0"=>$this->_translate->translate('noLabel'),"1"=>$this->_translate->translate('*'));
        $dbGrid = new DBGrid($view);
        $dbGrid->setPost($form);     
        $dbGrid->setSortOrder("id","desc");   
        $dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "products-title", $this->_translate->translate("title"));
        $dbGrid->addOptionsFilter(DBGridConstant::FILTER_TYPE_SELECT, "queries-query_type", $this->_translate->translate("queryType"), ConsultConstant::getConsultTypeList());
        $dbGrid->addOptionsFilter(DBGridConstant::FILTER_TYPE_SELECT, "queries-status", $this->_translate->translate("queryStatus"),ConsultConstant::getQueryStatusList());        
        $dbGrid->addFetchTable("queries", array("id","query_type as QueryType","status as QueryStatus","created as QueryTimer"));
//        $dbGrid->addFetchTable("product_items","upc as ProductNum", "product_items.id=queries.product_item_id");
//        $dbGrid->addFetchTable("product_items","upc as ProductNum", "product_items.id=queries.product_id");
        
        $dbGrid->addFetchTable("products","title as ProductName","products.id=queries.product_id");
        $dbGrid->addFetchTable("users","name as Username","users.id=queries.customer_id");
        $dbGrid->addValueFilter("queries-is_delete", "0");        
        $dbGrid->addFetchColumn("Username", $this->_translate->translate("username"),"edit");
        $dbGrid->addFetchColumn("ProductName", $this->_translate->translate("productTitle"));
        //$dbGrid->addFetchColumn("ProductNum", $this->_translate->translate("productnum"));
        $dbGrid->addFetchColumn("QueryType", $this->_translate->translate("queryType"),"",ConsultConstant::getConsultTypeList());
        $dbGrid->addFetchColumn("QueryStatus", $this->_translate->translate("queryStatus"),"",ConsultConstant::getQueryStatusList());
        $dbGrid->addFetchColumn("QueryTimer", $this->_translate->translate("queryTime"));
        //module action        
        $dbGrid->addAAModuleAction("consult","add",$this->_translate->translate("addBtn"),"add");
        $dbGrid->addAAModuleAction("consult", "edit", $this->_translate->translate("editBtn"), "edit");
        $dbGrid->addAAModuleAction("consult", "delete", $this->_translate->translate("delBtn"),"delete");
        $dbGrid->setPageTitle($this->_translate->translate("consultList"));
        $dbGrid->setModule('/consult/consult');
        return $dbGrid->render();
    }    
    
    //getQueryById
    public function getQueryById($id){
//       return $this->queryDao->getByCol("id",$id);
       return $this->queryDao->getQuery($id);
    }
    
    //getReplyByQueryId
    public function getReplyByQueryId($id){
//        return $this->queryreplyDao->fetchRow($id);
        return $this->queryreplyDao->getReplyInfoByQueryId($id);
    }    


}
?>
